/*============================================================================*/
/*                   CONTINENTAL AUTOMOTIVE SYSTEMS                           */
/*============================================================================*/
/*                        OBJECT SPECIFICATION                                */
/*============================================================================*
* name:            HALWipers.c
* version:         1.1
* created_by:      Rodrigo Casados, Lizette Acosta
* date_created:    June 7 2013
*=============================================================================*/
/* DESCRIPTION : Source file to implement main function.                      */
/*============================================================================*/
/* FUNCTION COMMENT : Implements entry point (main function) and other        */
/* application stuff.                                                         */
/*                                                                            */
/*============================================================================*/
#include "stdtypedef.h"
#include "HALPot.h"
#include "HALWipers.h"

T_UWORD test;

void WIPER_Init(void)
{
	InitPWM();
}



void vfn_WiperMode (void)
{
	vfn_ModeSelect();
}



void vfn_ModeSelect (void)
{
	static T_UBYTE u8_ButtonID;
	
	vfn_ReadButtons(&u8_ButtonID);	/* Reads the 4 buttons and writes the ID of the pressed one. 
									   Returns 0 if there's no event. */
		
	if(u8_ButtonID)
	{
		if(u8fn_Deboouncer(&u8_ButtonID))	/* Non-blocking debouncer for detected pressed button. */
		{
			vfn_SpeedModes (u8_ButtonID);
		}
	}
	
	else
	{
		vfn_SpeedModes (OFF);
	}
	
	
}


void vfn_ReadButtons(T_UBYTE * ptr_ButtonID)
{	
	if(BTN_1 || BTN_2 || BTN_3 || BTN_4)
	{
		if (BTN_1)
		{
			*ptr_ButtonID = 1;
		}
		
		else if(BTN_2)
		{
			*ptr_ButtonID = 2;
		}
		
		else if(BTN_3)
		{
			*ptr_ButtonID = 3;
		}
		
		else if(BTN_4)
		{
			*ptr_ButtonID = 4;
		}
		
	}	
}


	
T_UBYTE u8fn_Deboouncer(T_UBYTE * ptr_to_ButtonID)
{
	static T_UWORD u16_Cnt;
	static T_UBYTE u8_PrevButtonID = 0;
	T_UBYTE u8_ValidEvent;
	
	if(u8_PrevButtonID == *ptr_to_ButtonID)		/* Determines if the same button is being pressed. */
	{     									/* If it's not the same one, the counter resets.   */
     	if((u16_Cnt++) == DELAY_DEBAUNCER)
     	{
			 u16_Cnt = 0;					/* The counter is set to 0 for further debouncings.*/
			 u8_ValidEvent = 1;				/* After 10ms, the event is considered valid.	   */
			 u8_PrevButtonID = 0;			/* Invalid Button ID for further comparations.     */
    	}
    	
    	else
    	{
    		u8_ValidEvent = 0;
    	}
	}

	else
	{
		u16_Cnt = 0;
		u8_ValidEvent = 0;
	}
	
	u8_PrevButtonID = (T_UBYTE)(*ptr_to_ButtonID);
	
	return u8_ValidEvent;
}
	

void vfn_SpeedModes (T_UBYTE u8_SelectedMode)
{
	static T_UWORD u16_CurrentPosition = WIPER_MIN_ANGLE;
	static T_UBYTE u8_ReturnTo0 = 0;
	static T_UBYTE Prev_SelectedMode = 0;
	
	if((u8_SelectedMode != Prev_SelectedMode) || Prev_SelectedMode == 0)
	{
		if(u16_CurrentPosition == WIPER_MIN_ANGLE)
		{
			u8_ReturnTo0 = u8_SelectedMode;
		}
		
		else
		{
			u8_ReturnTo0 = Prev_SelectedMode;
		}
	}
	
	else if (u8_SelectedMode == Prev_SelectedMode)
	{
		u8_ReturnTo0 = u8_SelectedMode;
	}
	
	switch(u8_ReturnTo0)
	{
		case OFF:
		{
			LED_OFF_STATE();
			SetDutyCycle(WIPER_MIN_ANGLE);			
			break;
		}
		case SLOW:
		{
			LED_SLOW_STATE();
			WIPER_Speed(SLOW, &u16_CurrentPosition);
			SetDutyCycle(u16_CurrentPosition);	
			break;
		}
		case FAST:
		{
			LED_FAST_STATE();
			WIPER_Speed(FAST, &u16_CurrentPosition);
			SetDutyCycle(u16_CurrentPosition);			
			break;
		}
		
		case AUTO:
		{
			LED_AUTO_STATE();
						

			ProcessPotData(&u16_CurrentPosition);
			
			break;
		}
		default:
			break;
	}
	
	Prev_SelectedMode = u8_SelectedMode;
}

void WIPER_Speed(T_UBYTE u8_SelectedMode, T_UWORD *ptr_CurrentPosition)
{
	T_UWORD u16_DelayForSelectedMode;

	switch(u8_SelectedMode)
	{
		case SLOW:
			u16_DelayForSelectedMode = WIPER_SLOW;
		break;
		
		case FAST:
			u16_DelayForSelectedMode = WIPER_FAST;
		break;
		
		case FASTER:
			u16_DelayForSelectedMode = WIPER_FASTER;
		break;
	}
	
	if(*ptr_CurrentPosition == WIPER_MIN_ANGLE)
	{
		if(bfn_Delay(u16_DelayForSelectedMode))
		{
			*ptr_CurrentPosition = WIPER_MAX_ANGLE;
		}				
	}
			
	else if(*ptr_CurrentPosition == WIPER_MAX_ANGLE)
	{
		if(bfn_Delay(u16_DelayForSelectedMode))
		{
			*ptr_CurrentPosition = WIPER_MIN_ANGLE;
		}
	}
}


void WIPER_SetWipePos(void)
{	
	SetDutyCycle(POT_GetSensValue());				
}


void ProcessPotData(T_UWORD *u16_CurrentPosition)
{
	static T_UWORD su16_PotValue;
	
	while (!(ADCConvertionComplete()))
	{
		
	}
	
	su16_PotValue = ADCReadValue(32);
	
		
	if(su16_PotValue <500)
	{
		WIPER_Speed(SLOW, u16_CurrentPosition);
		SetDutyCycle(*u16_CurrentPosition);
	}
	
	if(su16_PotValue >500 && su16_PotValue < 1020)
	{
		WIPER_Speed(FAST, u16_CurrentPosition);
		SetDutyCycle(*u16_CurrentPosition);
	}
	
	if(su16_PotValue >1020 && su16_PotValue <= 1023)
	{
		WIPER_Speed(FASTER, u16_CurrentPosition);
		SetDutyCycle(*u16_CurrentPosition);
	}
}

